package com.zhsj.OkeHomeConsumption.ui.fragment.minehome;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.widget.SettingBar;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.ExtractBailBean;
import com.zhsj.OkeHomeConsumption.empty.ExtractBailItemBean;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.ui.fragment.minehome.mvp.ExtractBailPresenter;
import com.zhsj.OkeHomeConsumption.ui.fragment.minehome.mvp.ExtractBailUIInterFace;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   : 提取保证金
 */
public final class ExtractBailActivity extends MyActivity implements ExtractBailUIInterFace {

    @BindView(R.id.price)
    SettingBar price;
    @BindView(R.id.zhanghu)
    SettingBar zhanghu;
    @BindView(R.id.type)
    SettingBar type;
    @BindView(R.id.recy)
    RecyclerView recy;
    private ExtractBailPresenter mBailPresenter;
    //用户信息实体
    private FormalUserInfo userInfo = null;

    //保证金支付记录
    private List<ExtractBailBean> ExtractBailList = null;

    //保证金支付记录
    private List<ExtractBailItemBean> ExtractBaiitemlList = null;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_extractbail;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_copy_title;
    }

    @Override
    protected void initView() {
        userInfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        mBailPresenter = new ExtractBailPresenter(getActivity(), this);
    }

    @Override
    protected void initData() {
        ExtractBaiitemlList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recy.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mBailPresenter.BailGet(userInfo.getLifeUserDTO().getApplyCopartnerType() == 0 ? userInfo.getLifeUserDTO().getLifeType() : userInfo.getLifeUserDTO().getApplyCopartnerType());
    }

    @Override
    public void BailGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                if (jsonarr != null) {
                    ExtractBailList = new Gson().fromJson(jsonarr.toString(), new
                            TypeToken<ArrayList<ExtractBailBean>>() {
                            }.getType());
                    ExtractBailBean item = ExtractBailList.get(ExtractBailList.size()-1);
                    price.setRightText(item.getApplyRefundAmount()+"元");
                    zhanghu.setRightText(item.getDrawDepositAccount());
                    switch (item.getStatus()) {
                        case 0:
                            type.setRightText( "发起提取申请");
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount(),item.getCreateTime()
                            ,"发起提取申请","已将提取保证金申请提交到平台"));
                            break;
                        case 1:
                            type.setRightText("提取申请审核通过");
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount()
                                    ,item.getRatifyTime(),"提取申请审核通过","您发起的提取保证金已通过审核"));
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount(),item.getReviewTime()
                                    ,"发起提取申请","已将提取保证金申请提交到平台"));

                            break;
                        case 2:
                            type.setRightText("转账成功");
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount(),item.getRatifyTime(),"转账成功","平台已将"+item.getActualRefundAmount()+"元转入您的个人钱包，请在可提现金额里提取"));
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount(),item.getReviewTime(),"提取申请审核通过","您发起的提取保证金已通过审核"));
                            ExtractBaiitemlList.add(new ExtractBailItemBean(item.getApplyRefundAmount()+"",item.getActualRefundAmount(),item.getCreateTime(),"发起提取申请","已将提取保证金申请提交到平台"));
                            break;
                    }
                    ShopAdapter shopadper = new ShopAdapter();
                    recy.setAdapter(shopadper);
                }
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("BailGet", "获取保证金异常");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class ShopAdapter extends BaseQuickAdapter<ExtractBailItemBean, BaseViewHolder> {
        public ShopAdapter() {
            super(R.layout.item_extractbail, ExtractBaiitemlList);
        }

        @Override
        protected void convert(BaseViewHolder helper, final ExtractBailItemBean item) {
            if((ExtractBaiitemlList.size()-1)==helper.getLayoutPosition()){
                helper.setVisible(R.id.view,false);
            }
                    helper.setText(R.id.state_text, item.getTitle())
                            .setText(R.id.text, item.getStatestring())
                            .setText(R.id.timer_text, item.getTime());

        }
    }
}